﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using EffiProz.Core.Results;
using EffiProz.Core;
using EffiProz.Core.Lib;
using EffiProz.Core.Navigators;

namespace EffiProz.Test
{
    /// <summary>
    /// Summary description for IndexTest
    /// </summary>
    [TestClass]
    public class IndexTest : BaseTest
    {
        public IndexTest()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void CreateIndex()
        {
            Result resultIn;

            string table = TestHelper.GetNewTableName();
            string sql = "CREATE TABLE " + table + " (col1 int , col2 varchar(32), col3 int)";
            resultIn = TestHelper.ExecuteUpdateSQL_Success(sessionProxy, sql);

            int col1val = 0;
            string col2val = "test1";
            int col3val = 4;
            sql = TestHelper.CreateStdInsert(table, 3, col1val, col2val, col3val);
            resultIn = TestHelper.ExecuteUpdateSQL_Success(sessionProxy, sql);
            sql = TestHelper.CreateStdSelect(table, 3);
            resultIn = TestHelper.ExecuteSelectSQL(sessionProxy, sql, 1);

            col1val = 1;
            col2val = "test2";
            col3val = 5;
            sql = TestHelper.CreateStdInsert(table, 3, col1val, col2val, col3val);
            resultIn = TestHelper.ExecuteUpdateSQL_Success(sessionProxy, sql);
            sql = TestHelper.CreateStdSelect(table, 3);
            resultIn = TestHelper.ExecuteSelectSQL(sessionProxy, sql, 2);


            sql = "CREATE INDEX " + table + "INDEX ON " + table + " (col2)"; //single colum index
            resultIn = TestHelper.ExecuteUpdateSQL_Success(sessionProxy, sql);
            col1val = 3;
            col2val = "test32";
            col3val = 8;
            sql = TestHelper.CreateStdInsert(table, 3, col1val, col2val, col3val);
            resultIn = TestHelper.ExecuteUpdateSQL_Success(sessionProxy, sql);
            sql = TestHelper.CreateStdSelect(table, 3);
            resultIn = TestHelper.ExecuteSelectSQL(sessionProxy, sql, 3);


            sql = "CREATE INDEX " + table + "INDEX2 ON " + table + " (col1,col3)"; //single colum index
            resultIn = TestHelper.ExecuteUpdateSQL_Success(sessionProxy, sql);
            col1val = 4;
            col2val = "test2";
            col3val = 9;
            sql = TestHelper.CreateStdInsert(table, 3, col1val, col2val, col3val);
            resultIn = TestHelper.ExecuteUpdateSQL_Success(sessionProxy, sql);
            sql = TestHelper.CreateStdSelect(table, 3);
            resultIn = TestHelper.ExecuteSelectSQL(sessionProxy, sql, 4);

            //rename index
            sql = "ALTER INDEX " + table + "INDEX2 RENAME TO " + table + "INDEX2NEW;"; //single colum index
            resultIn = TestHelper.ExecuteUpdateSQL_Success(sessionProxy, sql);

            //drop index
            sql = "DROP INDEX " + table + "INDEX;"; //single colum index
            resultIn = TestHelper.ExecuteUpdateSQL_Success(sessionProxy, sql);

            //check an insert
            col1val = 5;
            col2val = "test5";
            col3val = 6666;
            sql = TestHelper.CreateStdInsert(table, 3, col1val, col2val, col3val);
            resultIn = TestHelper.ExecuteUpdateSQL_Success(sessionProxy, sql);
            sql = TestHelper.CreateStdSelect(table, 3);
            resultIn = TestHelper.ExecuteSelectSQL(sessionProxy, sql, 5);

        }

        [TestMethod]
        public void TestMissingFromClause()
        {
            Result resultIn;
            string sql = "SELECT 5+8 as myval FROM INFORMATION_SCHEMA.SYSTEM_CATALOGS;";

            resultIn = TestHelper.ExecuteSelectSQL(sessionProxy, sql, 1);
        }

        [TestMethod]
        public void TestReservedWordInString()
        {
            Result resultIn;
            string sql = "SELECT 'PRIMARY KEY' as myval, true as valb FROM INFORMATION_SCHEMA.SYSTEM_CATALOGS;";

            resultIn = TestHelper.ExecuteSelectSQL(sessionProxy, sql, 1);
        }


    }
}
